//
//  ParallaxHeaderView.m
//  ParallaxTableViewHeader
//
//  Created by Vinodh  on 26/10/14.
//  Copyright (c) 2014 Daston~Rhadnojnainva. All rights reserved.

//

#import <QuartzCore/QuartzCore.h>

#import "ParallaxHeaderView.h"
#import "UIImage+ImageEffects.h"
#import "UIImageView+WebCache.h"

@interface ParallaxHeaderView ()
@property (weak, nonatomic) IBOutlet UIScrollView *imageScrollView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic) IBOutlet UIImageView *bluredImageView;

@end

#define kDefaultHeaderFrame CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)


static CGFloat kParallaxDeltaFactor = 0.5f;
static CGFloat kMaxTitleAlphaOffset = 100.0f;
static CGFloat kLabelPaddingDist = 8.0f;

@implementation ParallaxHeaderView


+ (id)parallaxHeaderViewWithImageURL:(NSString *)imageURL name:(NSString *)name age:(NSString *)age gender:(NSString *)gender forSize:(CGSize)headerSize {
    ParallaxHeaderView *headerView = [[ParallaxHeaderView alloc] initWithFrame:CGRectMake(0, 0, headerSize.width, headerSize.height)];
    
    
    
    [headerView setHeaderViewWithImageURL:imageURL name:name age:age gender:gender];
    
    return headerView;
}

+ (id)parallaxHeaderViewWithImage:(UIImage *)image forSize:(CGSize)headerSize {
    ParallaxHeaderView *headerView = [[ParallaxHeaderView alloc] initWithFrame:CGRectMake(0, 0, headerSize.width, headerSize.height)];
    headerView.headerImage = image;
    [headerView initialSetup];
    return headerView;
    
}

+ (id)parallaxHeaderViewWithCGSize:(CGSize)headerSize
{
    ParallaxHeaderView *headerView = [[ParallaxHeaderView alloc] initWithFrame:CGRectMake(0, 0, headerSize.width, headerSize.height)];
    [headerView initialSetup];
    return headerView;
}

- (void)awakeFromNib
{
    [self initialSetup];
    [self refreshBlurViewForNewImage];
}
/**
 *  设置headerView的子控件
 *
 */
- (void)setHeaderViewWithImageURL:(NSString *)imageURL name:(NSString *)name age:(NSString *)age gender:(NSString *)gender {
    // 实例化一个imageView
    UIImageView *iconView = [[UIImageView alloc] init];
    [iconView sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    CGFloat iconX = kDefaultHeaderMargin;
    CGFloat iconY = iconX;
    CGFloat iconW = 100;
    CGFloat iconH = iconW;
    iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    iconView.layer.cornerRadius = iconW * 0.5;
    iconView.clipsToBounds = YES;
//    iconView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:iconView];
    
    UIView *view =[[UIView alloc] init];
    CGFloat viewX = CGRectGetMaxX(iconView.frame) + kDefaultHeaderMargin;
    CGFloat viewY = iconY;
    CGFloat viewW = self.width - iconW - 2 * kDefaultHeaderMargin;
    CGFloat viewH = CGRectGetMaxY(iconView.frame);
    view.frame = CGRectMake(viewX, viewY, viewW, viewH);
    [self addSubview:view];
    
    // 创建nameLabel
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = name;
    nameLabel.font = kDefaultFont;
    nameLabel.textColor = [UIColor whiteColor];
    [nameLabel setTextAlignment:NSTextAlignmentCenter];
    nameLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    CGFloat nameX = kDefaultHeaderMargin;
    CGFloat nameY = kDefaultHeaderMargin;
    CGSize nameSize = [name sizeWithFont:kDefaultFont];
    nameLabel.frame = (CGRect){{nameX, nameY}, nameSize};
    [view addSubview:nameLabel];
    
    // 创建年龄label
    UILabel *ageLabel = [[UILabel alloc] init];
    ageLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    NSString *newAge = [NSString stringWithFormat:@"%@岁", age];
    ageLabel.text = newAge;
    ageLabel.font = kDefaultFont;
    ageLabel.textColor = [UIColor whiteColor];
    [ageLabel setTextAlignment:NSTextAlignmentCenter];
    CGFloat ageX = kDefaultHeaderMargin;
    CGFloat ageY = CGRectGetMaxY(nameLabel.frame) + kDefaultHeaderMargin;
    CGSize ageSize = [newAge sizeWithFont:kDefaultFont];
    ageLabel.frame = (CGRect){{ageX, ageY}, ageSize};
    [view addSubview:ageLabel];
    
    // 创建性别label
    UILabel *genderLabel = [[UILabel alloc] init];
    genderLabel.text = gender;
    genderLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    genderLabel.font = kDefaultFont;
    genderLabel.textColor = [UIColor whiteColor];
    [genderLabel setTextAlignment:NSTextAlignmentCenter];
    CGFloat genderX = CGRectGetMaxX(ageLabel.frame) + kDefaultHeaderMargin;
    CGFloat genderY = ageY;
    CGSize genderSize = [gender sizeWithFont:kDefaultFont];
    genderLabel.frame = (CGRect){{genderX, genderY}, genderSize};
    [view addSubview:genderLabel];
}

- (void)layoutHeaderViewForScrollViewOffset:(CGPoint)offset
{
    CGRect frame = self.imageScrollView.frame;
    
    if (offset.y > 0)
    {
        frame.origin.y = MAX(offset.y *kParallaxDeltaFactor, 0);
        self.imageScrollView.frame = frame;
        self.bluredImageView.alpha =   1 / kDefaultHeaderFrame.size.height * offset.y * 2;
        self.clipsToBounds = YES;
    }
    else
    {
        CGFloat delta = 0.0f;
        CGRect rect = kDefaultHeaderFrame;
        delta = fabs(MIN(0.0f, offset.y));
        rect.origin.y -= delta;
        rect.size.height += delta;
        self.imageScrollView.frame = rect;
        self.clipsToBounds = NO;
        self.headerTitleLabel.alpha = 1 - (delta) * 1 / kMaxTitleAlphaOffset;
    }
}

#pragma mark -
#pragma mark Private

- (void)initialSetup
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.imageScrollView = scrollView;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:scrollView.bounds];
    imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.image = self.headerImage;
    self.imageView = imageView;
    [self.imageScrollView addSubview:imageView];
    
    CGRect labelRect = self.imageScrollView.bounds;
    labelRect.origin.x = labelRect.origin.y = kLabelPaddingDist;
    labelRect.size.width = labelRect.size.width - 2 * kLabelPaddingDist;
    labelRect.size.height = labelRect.size.height - 2 * kLabelPaddingDist;
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:labelRect];
    headerLabel.textAlignment = NSTextAlignmentCenter;
    headerLabel.numberOfLines = 0;
    headerLabel.lineBreakMode = NSLineBreakByWordWrapping;
    headerLabel.autoresizingMask = imageView.autoresizingMask;
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.font = [UIFont fontWithName:@"AvenirNextCondensed-Regular" size:23];
    self.headerTitleLabel = headerLabel;
    [self.imageScrollView addSubview:self.headerTitleLabel];
    
    self.bluredImageView = [[UIImageView alloc] initWithFrame:self.imageView.frame];
    self.bluredImageView.autoresizingMask = self.imageView.autoresizingMask;
    self.bluredImageView.alpha = 0.0f;
    [self.imageScrollView addSubview:self.bluredImageView];
    
    [self addSubview:self.imageScrollView];
    
    [self refreshBlurViewForNewImage];
}

- (void)setHeaderImage:(UIImage *)headerImage
{
    _headerImage = headerImage;
    self.imageView.image = headerImage;
    [self refreshBlurViewForNewImage];
}

- (UIImage *)screenShotOfView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(kDefaultHeaderFrame.size, YES, 0.0);
    [self drawViewHierarchyInRect:kDefaultHeaderFrame afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (void)refreshBlurViewForNewImage
{
    UIImage *screenShot = [self screenShotOfView:self];
    screenShot = [screenShot applyBlurWithRadius:5 tintColor:[UIColor colorWithWhite:0.6 alpha:0.2] saturationDeltaFactor:1.0 maskImage:nil];
    self.bluredImageView.image = screenShot;
}

@end
